package com.zhiyou.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.model.IllegalMappingException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.mapper.ShareMapper;
import com.zhiyou.model.Like;
import com.zhiyou.model.Share;
import com.zhiyou.model.ShareImg;
import com.zhiyou.service.ShareService;
import com.zhiyou.utils.FTPUtil;

@Service
public class ShareServiceImpl implements ShareService {

	@Autowired
	ShareMapper shareMapper;

	@Transactional
	@Override
	public void addShare(Share share, MultipartFile[] files) {
		// 插入说说，获取到该说说的主键
		shareMapper.addShare(share);
		if (files != null) {
			for (MultipartFile multipartFile : files) {
				// 把文件放在服务器上
				String image = FTPUtil.setImage(multipartFile);
				// 创建说说图片的model，并且把图片地址放进去
				ShareImg shareImg = new ShareImg();
				shareImg.setShare_id(share.getShare_id());
				shareImg.setShare_img_url(image);
				// 放进数据库
				shareMapper.addShareImg(shareImg);
			} 
		}
	}

	@Override
	public void addLike(Like like) {
		shareMapper.addLike(like);
	}

	@Transactional
	@Override
	public List<Share> selectAll(int id) {
		List<Share> list = shareMapper.selectAll(id);
		for (int i = 0; i < list.size(); i++) {
			int share_id = list.get(i).getShare_id();
			list.get(i).setShareImgs(shareMapper.selectImgById(share_id));
			list.get(i).setLikes(shareMapper.selectLikeById(share_id));
		}
		return list;
	}

	@Transactional
	@Override
	public Share selectById(int id) {
		Share share = shareMapper.selectShareById(id);
		share.setShareImgs(shareMapper.selectImgById(id));
		share.setLikes(shareMapper.selectLikeById(id));
		return share;
	}

	@Transactional
	@Override
	public List<Like> selectLikeById(int id) {
		return shareMapper.selectLikeById(id);
	}

	@Transactional
	@Override
	public void deleteShare(int id) {
		// 删除说说
		shareMapper.deleteShare(id);
		// 获取到该说说的所有图片信息
		List<ShareImg> list = shareMapper.selectImgById(id);
		// 把服务器上的图片删除
		for (ShareImg shareImg : list) {
			FTPUtil.rmImage(shareImg.getShare_img_url());
		}
		// 把说说图片数据库的记录删掉
		shareMapper.deleteShareImg(id);
		// 删除说说的点赞
		shareMapper.deleteLike(id);
	}

	@Transactional
	@Override
	public List<Share> selectAllByPar_id(int id) {
		List<Share> list = shareMapper.selectAllByPar_id(id);
		for (int i = 0; i < list.size(); i++) {
			int share_id = list.get(i).getShare_id();
			list.get(i).setShareImgs(shareMapper.selectImgById(share_id));
			list.get(i).setLikes(shareMapper.selectLikeById(share_id));
		}
		return list;
	}

	@Override
	public void updaet(Share share) {
		shareMapper.update(share);
	}

	@Transactional
	@Override
	public void deleteShareAll(int[] ids) {
		shareMapper.deleteShareAll(ids);
		shareMapper.deleteLikeAll(ids);
		shareMapper.deleteShareImgAll(ids);
	}

}
